home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_135.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  1.8 KB  |  38 lines

  1.  
  2. Control Structures - Return
  3.     
  4.     In all the previous examples, the result of an sequence of expressions has been the value of the last expression.  We have been taking advantage of a shortcut offered in workspaces: if there is no return statement, they return the result of the the last expression.  Elsewhere in the system this isn‚Äôt so: you need to have a return statement at the end if you want to return the last value.  Furthermore, sometimes you want to return a value from the middle of a sequence of expressions.
  5.  
  6.     A return statement causes the result of an expression to become the result of a sequence of expressions, and also stops all further execution of the whole sequence.
  7.  
  8.         $ income, payment, monthlies.
  9.         income := 28500 / 12.
  10.         payment := 872.50.
  11.         monthlies := 200 + payment.
  12.         if monthlies / income > 0.50
  13.             then [ return "Monthly payments are too high." ].
  14.         if payment / income > 0.30
  15.             then [ return "Payment is too high." ].
  16.         return "This mortgage is OK".
  17.  
  18.     If either of the blocks gets executed (because one of the conditions is true) then a string will be returned and the rest of the sequence not executed.
  19.  
  20.     The word return can appear before any expression.  Since it is not a message, return can appear before a message without the message having to be in parentheses:
  21.  
  22.   if (choose 1 to 100) > 50
  23.             then [ return "aeiou" choose ]
  24.             else [ return "bcdfghjklmnpqrstvwxyz" choose ].
  25.  
  26.  
  27. Return Without a Value
  28.     If you just want to stop execution without any result, you can use return just by itself.  The expression will not have a result.  In a workspace, the result will show as ‚ÄúNo result‚Äù.
  29.  
  30.         a := choose 1 to 6.
  31.         b := choose 1 to 6.
  32.         if (a == 1 and b == 1)
  33.             then [ return ].        snake eyes, we‚Äôre done.
  34.         a := choose 1 to 6.
  35.         b := choose 1 to 6.
  36.         return.
  37.  
  38.